package com.inspur.xiaoju.service.impl;

import com.inspur.xiaoju.constant.RecordStatus;
import com.inspur.xiaoju.dto.CreateSurveyResponseDto;
import com.inspur.xiaoju.exception.BusinessException;
import com.inspur.xiaoju.model.SurveyResponse;
import com.inspur.xiaoju.repository.SurveyResponseRepository;
import com.inspur.xiaoju.security.PluginManager;
import com.inspur.xiaoju.service.SurveyResponseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class SurveyResponseServiceImpl implements SurveyResponseService {

    @Autowired
    private SurveyResponseRepository surveyResponseRepository;
    
    @Autowired
    private PluginManager pluginManager;
    
    @Override
    public SurveyResponse create(CreateSurveyResponseDto createDto) {
        SurveyResponse response = new SurveyResponse();
        response.setSurveyId(createDto.getSurveyId());
        response.setSurveyPath(createDto.getSurveyPath());
        response.setData(createDto.getData());
        
        // 设置客户端时间和时间差
        Date now = new Date();
        Long serverTime = now.getTime();
        Long clientTime = createDto.getClientTime();
        if (clientTime != null) {
            response.setClientTime(clientTime);
            response.setDiffTime(serverTime - clientTime);
        }
        
        // 设置状态
        SurveyResponse.Status status = new SurveyResponse.Status();
        status.setStatus(RecordStatus.NEW);
        status.setDate(now);
        
        response.setCurStatus(status);
        
        List<SurveyResponse.Status> statusList = new ArrayList<>();
        statusList.add(status);
        response.setStatusList(statusList);
        
        response.setCreateDate(now);
        response.setUpdateDate(now);
        
        // 加密敏感数据
        pluginManager.encryptResponseData(response);
        
        return surveyResponseRepository.save(response);
    }
    
    @Override
    public SurveyResponse findById(String id) {
        SurveyResponse response = surveyResponseRepository.findById(id)
                .orElseThrow(() -> new BusinessException(404, "问卷回答不存在"));
        
        // 解密敏感数据
        pluginManager.decryptResponseData(response);
        
        return response;
    }
    
    @Override
    public List<SurveyResponse> findBySurveyId(String surveyId) {
        List<SurveyResponse> responses = surveyResponseRepository.findBySurveyId(surveyId);
        
        // 解密每个响应中的敏感数据
        for (SurveyResponse response : responses) {
            pluginManager.decryptResponseData(response);
        }
        
        return responses;
    }
    
    @Override
    public List<SurveyResponse> findBySurveyPath(String surveyPath) {
        List<SurveyResponse> responses = surveyResponseRepository.findBySurveyPath(surveyPath);
        
        // 解密每个响应中的敏感数据
        for (SurveyResponse response : responses) {
            pluginManager.decryptResponseData(response);
        }
        
        return responses;
    }
}